#!/usr/bin/env bash

set -e

usage() {
    echo "Usage: $0 [-t <tag>] [-s <scala_version>]" 1>&2
    echo "Example: $0 -t v0.13.3 -s 2.13.14"
}

while getopts "s:t:" OPTION; do
    case $OPTION in
	s)
            SCALA_VERSION=$OPTARG
            ;;
	t)
            TAG=$OPTARG
            ;;
	*)
            usage
            exit 1
            ;;
    esac
done

if [ -z "$SCALA_VERSION" ] || [ -z "$TAG" ]; then
    usage
    exit 1
fi

JAVA_VERSION=$(java -version 2>&1 | head -1 | cut -d'"' -f2 | sed '/^1\./s///' | cut -d'.' -f1)
if [ "$JAVA_VERSION" -ne 8 ]; then
    echo "Detected Java ${JAVA_VERSION}, must be 8."
    exit 1
fi

git checkout $TAG
sbt ++$SCALA_VERSION! clean test publish sonaRelease
